package com.hujing;

import java.util.concurrent.CompletableFuture;

/**
 * @Author : hujing
 * @Date: 2025/7/28 10:25
 * @Description: 线程不安全的例子
 */
public class ThreadDemo2 {
    
    private static int sum = 0;

    public static void main(String[] args) {
        Thread t1 = new Thread(ThreadDemo2::add);
        Thread t2 = new Thread(ThreadDemo2::subtract);
        
        t1.start();
        t2.start();
        
        try{
            t1.join();
            t2.join();
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        System.out.println("Sum is: " + sum);
    }
    
    public static void add(){
        for (int i= 0;i<1000;i++){
            sum += i;
        }
    }
    
    public static void subtract(){
        for (int i= 0;i<1000;i++){
            sum -= i;
        }
    }
}
